import { ref } from "vue";
import { ElMessage } from "element-plus";
import { deleteBodyAPI, getBodyListAPI } from "@/api/resource.ts";

export default function useBody() {
  const dialog = ref()
  const viewerVisible = ref(false);
  const bodyList = ref([])
  const currentImage = ref('');

  // 获取部位列表
  const getBodyList = async () => {
    const res = await getBodyListAPI()
    bodyList.value = res.data
  }

  // 图片预览
  const previewImage = (image: any) => {
    currentImage.value = image;
    viewerVisible.value = true;
  }

  // 关闭图片预览
  const closeImageViewer = () => {
    viewerVisible.value = false;
  };

  // 添加部位
  const addBody = () => {
    dialog.value.init({}, 0)
  }

  // 编辑部位
  const editBody = (row: any) => {
    dialog.value.init(row, 1)
  }

  // 删除部位
  const deleteBody = async (id: string) => {
    const res: any = await deleteBodyAPI(id)
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: res.message
      })
      await getBodyList()
    }
  }

  return {
    dialog,
    bodyList,
    viewerVisible,
    currentImage,
    previewImage,
    closeImageViewer,
    getBodyList,
    addBody,
    editBody,
    deleteBody
  }
}
